/*
 * Copyright (C) 拾光分享网 
 * All rights reserved, Designed By www.hiai.top.
 * Email：goodsking@163.com.
 * Version：V1.0.2(20230701).
 */
package top.hiai._admin.permission;

import com.jfinal.plugin.activerecord.Page;
import top.hiai.common.model.Permission;

import java.util.List;

/**
* @Author www.hiai.top
* @Email  goodsking@163.com
* @Message 更多资源尽在www.hiai.top
* @Introduction 类的介绍
*/

public class PermissionAdminService {
	
	public static final PermissionAdminService me = new PermissionAdminService();
	private Permission permissionDao = new Permission().dao();
	
	/**
	 * 根据ID查找
	 * @param id
	 * @return
	 */
	public Permission findById(int id) {
		return permissionDao.findById(id);
	}
	
	/**
	 * 获取全部的菜单，不分页
	 * 用于分配角色的时候遍历
	 * @return
	 */
	public List<Permission> getAllPermissions() {
		return permissionDao.find("select * from bbs_permission order by controller asc");
	}
	
	/**
	 * 分页显示菜单，用于查看
	 * @param pageNum
	 * @return
	 */
	public Page<Permission> paginate(int pageNum,int pageSize) {
		return permissionDao.paginate(pageNum, pageSize, "select *", "from bbs_permission order by actionKey asc");
	}
	
	/**
	 * 替换控制器前缀，界面显示时更加美观
	 *
	 * 例子：
	 * replaceControllerPrefix(permissionPage, "com.jfinal.club._admin.", "...");
	 * 以上例子将 "com.jfinal.club._admin." 这一长串前缀替换成 "..."，显示更美观
	 */
	public void replaceControllerPrefix(Page<Permission> permissionPage, String replaceTarget, String replacement) {
		for (Permission p : permissionPage.getList()) {
			String c = p.getController().replace(replaceTarget, replacement);
			p.setController(c);
		}
	}
	
	/**
	 * 修改菜单
	 * @param id
	 * @param remark
	 * @return
	 */
	public Boolean permissionEdit(int id,String remark){
		Permission permission = new Permission();
		permission.setId(id);
		permission.setRemark(remark);
		if(permission.update()){
			return true;
		}else{
			return false;
		}
	}
	
}